#!/usr/bin/env bash

PREFIX=$(realpath $(cd "$(dirname "$0")"; pwd))

export NODE_ENV=production

cd $PREFIX

if ! hash parcel 2>/dev/null; then
yarn global add parcel-bundler
fi

if [ ! -d "$PREFIX/dist" ]; then
mkdir -p $PREFIX/dist
fi

cd $PREFIX/dist
rm -rf *.css *.html *.js

cd $PREFIX
yarn run build

if ! hash uglifyjs 2>/dev/null; then
yarn global add uglify-js
fi
coffee -o dist/ -c sw/
uglifyjs dist/sw.js -cmo dist/sw.js

cd $PREFIX/dist
git add .

sync() {
    git add -u && git commit -m '.' ;
    local branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' |awk -F' ' '{print $2}'`
    git pull origin $branch && git push origin $branch;
}

sync
